# Define your item pipelines here
#
# Don't forget to add your pipeline to the ITEM_PIPELINES setting
# See: https://docs.scrapy.org/en/latest/topics/item-pipeline.html

from openpyxl import Workbook
import pymysql
# useful for handling different item types with a single interface
from itemadapter import ItemAdapter

class DoubanPipeline(object):
    #创建表
    def __init__(self):
        self.wb = Workbook()
        self.ws = self.wb.active
        self.ws.append(['编号', '中文名', '其他名','导演主演','评价','年份风格','内容'])
    def process_item(self, item, spider):
        #添加到item到表
        line=[item['film_id'],item['film_CNname'],item['film_OTname'],item['film_introduce'],item['film_comments'],item['film_style'],item['film_prime']]
        self.ws.append(line)
        self.wb.save('./doubanTOP1.xlsx')
        return item

# class doubanmysqlPipeline(object):
#     def __init__(self):
#         # connection database
#         self.connect = pymysql.connect(host='localhost', user='root', passwd='zs233',db='doubantop')  # 后面三个依次是数据库连接名、数据库密码、数据库名称
#         # get cursor
#         self.cursor = self.connect.cursor()
#         print("连接数据库成功")
#     def process_item(self, item, spider):
#         insert_sql="""
#         insert into douban_TOP(film_id,film_CNname,film_OTname,film_introduce,film_comments,film_style,film_prime)VALUES(%d,%s,%s,%s,%s,%s,%s)
#         """
#         self.cursor.execute(insert_sql,(item['film_id'],item['film_CNname'],item['film_OTname'],item['film_introduce'],item['film_comments'],item['film_style'],item['film_prime']))
#         self.connect.commit()
#     def close_spider(self, spider):
#         # 关闭游标和连接
#         self.cursor.close()
#         self.connect.close()